package com.gitee.qdbp.jdbc.support;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConversionService;
import com.gitee.qdbp.jdbc.api.QdbcBoot;
import com.gitee.qdbp.jdbc.api.SqlBufferJdbcOperations;
import com.gitee.qdbp.jdbc.biz.QdbcBootImpl;
import com.gitee.qdbp.jdbc.plugins.DbPluginContainer;
import com.gitee.qdbp.jdbc.plugins.impl.SimpleSqlFragmentOptions;

/**
 * QdbcBootFactoryBean<br>
 * 同时负责数据库插件初始化处理
 *
 * @author zhaohuihua
 * @version 20200128
 * @deprecated 20201124 3.2.10<br>
 *             由于QdbcBoot对于每个数据源都会有一个实例, 而DbPluginContainer全局只有一个实例<br>
 *             此二者放在一起初始化并不合适, 会导致多次执行DbPluginContainer的初始化<br>
 *             因此将DbPluginContainer的初始化独立抽取为QdbcBootSpringInitializer<br>
 * @see QdbcBootSpringInitializer
 */
@Deprecated
public class QdbcBootFactoryBean implements FactoryBean<QdbcBoot>, InitializingBean, ApplicationContextAware {

    private boolean singleton = true;
    private boolean initialized = false;
    private QdbcBootImpl singletonInstance;

    private ApplicationContext context;

    /** Spring的类型转换处理类 **/
    private ConversionService conversionService;
    /** 数据库插件容器类 **/
    private DbPluginContainer pluginContainer;
    /** SqlBuffer数据库操作类 **/
    private SqlBufferJdbcOperations sqlBufferJdbcOperations;
    /** 是否在系统启动时扫描SQL文件 **/
    @Deprecated // 移至SimpleSqlFragmentOptions
    private Boolean sqlTemplateScanOnStartup;

    @Override
    public void afterPropertiesSet() {
        if (singletonInstance != null) {
            singletonInstance.setSqlBufferJdbcOperations(sqlBufferJdbcOperations);
        }
        if (pluginContainer == null) {
            pluginContainer = new DbPluginContainer();
        }
        if (conversionService != null && pluginContainer.getConversionService() == null) {
            pluginContainer.setConversionService(conversionService);
        }
        if (sqlTemplateScanOnStartup != null && pluginContainer.getSqlFragmentOptions() == null) {
            SimpleSqlFragmentOptions sqlFragmentOptions = new SimpleSqlFragmentOptions();
            sqlFragmentOptions.setSqlTemplateScanOnStartup(sqlTemplateScanOnStartup);
            pluginContainer.setSqlFragmentOptions(sqlFragmentOptions);
        }
        QdbcBootSpringInitializer initializer = new QdbcBootSpringInitializer();
        initializer.setApplicationContext(context);
        initializer.setPluginContainer(pluginContainer);
        initializer.afterPropertiesSet();
    }

    /** 获取实例 **/
    @Override
    public QdbcBoot getObject() {
        if (isSingleton()) {
            if (!this.initialized) {
                this.singletonInstance = createInstance();
                this.initialized = true;
            }
            return this.singletonInstance;
        } else {
            return createInstance();
        }
    }

    @Override
    public Class<?> getObjectType() {
        return QdbcBoot.class;
    }

    protected QdbcBootImpl createInstance() {
        QdbcBootImpl boot = new QdbcBootImpl();
        boot.setSqlBufferJdbcOperations(sqlBufferJdbcOperations);
        return boot;
    }

    /** 判断是否使用单例模式 **/
    @Override
    public boolean isSingleton() {
        return singleton;
    }

    /** 设置是否使用单例模式 **/
    public void setSingleton(boolean singleton) {
        this.singleton = singleton;
    }

    @Override
    public void setApplicationContext(ApplicationContext context) {
        this.context = context;
    }

    public ApplicationContext getApplicationContext() {
        return context;
    }

    /** Spring的类型转换处理类 **/
    public ConversionService getConversionService() {
        return conversionService;
    }

    /** Spring的类型转换处理类 **/
    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    /** SqlBuffer数据库操作类 **/
    public SqlBufferJdbcOperations getSqlBufferJdbcOperations() {
        return sqlBufferJdbcOperations;
    }

    /** SqlBuffer数据库操作类 **/
    public void setSqlBufferJdbcOperations(SqlBufferJdbcOperations sqlBufferJdbcOperations) {
        this.sqlBufferJdbcOperations = sqlBufferJdbcOperations;
    }

    /** 是否在系统启动时扫描SQL文件 **/
    public Boolean isSqlTemplateScanOnStartup() {
        return sqlTemplateScanOnStartup;
    }

    /** 是否在系统启动时扫描SQL文件 **/
    public void setSqlTemplateScanOnStartup(Boolean sqlTemplateScanOnStartup) {
        this.sqlTemplateScanOnStartup = sqlTemplateScanOnStartup;
    }

    /** 数据库插件容器类 **/
    public DbPluginContainer getPluginContainer() {
        return pluginContainer;
    }

    /** 数据库插件容器类 **/
    public void setPluginContainer(DbPluginContainer pluginContainer) {
        this.pluginContainer = pluginContainer;
    }

}
